package org.third.security.crypto;

import org.junit.Test;
import org.third.common.utils.CodecUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MessageDigestTest {

	private static final String HASH_ALGORITHM = "SHA-256";

	@Test
	public void testIdMDbUser() {
//		password                                         salt
//		4omkiYlW1AVTyjmGBb5MMH2pUoiScrY00A4sS8XHpmE=	+7iN6XgCBjZGifHfkfXQJWUAeO6israwEngdwqj7L1Q=
//		Q6GudYhmj2aRCrmBc+RvaUu1fMPqbMIAjCaN4lcyi3M=	Y5Oo0hsVWbbKALVJm1Onk59vCs7WFPAlBFbZlAP7Yh8=
		String rawPassword = "abc", salt = "+7iN6XgCBjZGifHfkfXQJWUAeO6israwEngdwqj7L1Q=", hashPassword="B94ELVITIdLe+UY9w2X/E9pE+b0IgX//Wp+gRE9Itis=";
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance(HASH_ALGORITHM);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		md.update(CodecUtils.decodeBase64(salt));
		byte[] messageBytes = rawPassword.toString().getBytes();
		md.update(messageBytes);
		byte[] disgestedPassword = md.digest();

		String password = CodecUtils.encodeBase64String(disgestedPassword);
//		ae50b06c0f7c94b8171064ce9a2e27843e0a229bc6b0f4a81c8bf97d84661191
		System.out.println(password);
	}

	@Test
	public void testEncode() {
		System.out.println(new String(CodecUtils.decodeHex("ae50b06c0f7c94b8171064ce9a2e27843e0a229bc6b0f4a81c8bf97d84661191")));
	}
}
